home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / CharArrayReader.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.1 KB  |  202 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)CharArrayReader.java    1.12 98/06/05
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * This class implements a character buffer that can be used as a
  19.  * character-input stream.
  20.  *
  21.  * @author    Herb Jellinek
  22.  * @version     1.12, 06/05/98
  23.  * @since       JDK1.1
  24.  */
  25. public
  26. class CharArrayReader extends Reader {
  27.     /** Character buffer */
  28.     protected char buf[];
  29.  
  30.     /** Current buffer position */
  31.     protected int pos;
  32.  
  33.     /** Position of mark in buffer */
  34.     protected int markedPos = 0;
  35.  
  36.     /** Number of valid characters in buffer */
  37.     protected int count;
  38.  
  39.     /**
  40.      * Create an CharArrayReader from the specified array of chars.
  41.      * @param buf    Input buffer (not copied)
  42.      */
  43.     public CharArrayReader(char buf[]) {
  44.     this.buf = buf;
  45.         this.pos = 0;
  46.     this.count = buf.length;
  47.     }
  48.  
  49.     /**
  50.      * Create an CharArrayReader from the specified array of chars.
  51.      * @param buf    Input buffer (not copied)
  52.      * @param offset    Offset of the first char to read
  53.      * @param length    Number of chars to read
  54.      */
  55.     public CharArrayReader(char buf[], int offset, int length) {
  56.     if ((offset < 0) || (offset > buf.length) || (length < 0) ||
  57.             ((offset + length) < 0)) {
  58.         throw new IllegalArgumentException();
  59.     }
  60.     this.buf = buf;
  61.         this.pos = offset;
  62.     this.count = Math.min(offset + length, buf.length);
  63.         this.markedPos = offset;
  64.     }
  65.  
  66.     /** Check to make sure that the stream has not been closed */
  67.     private void ensureOpen() throws IOException {
  68.     if (buf == null)
  69.         throw new IOException("Stream closed");
  70.     }
  71.  
  72.     /**
  73.      * Read a single character.
  74.      * 
  75.      * @exception   IOException  If an I/O error occurs
  76.      */
  77.     public int read() throws IOException {
  78.     synchronized (lock) {
  79.         ensureOpen();
  80.         if (pos >= count)
  81.         return -1;
  82.         else
  83.         return buf[pos++];
  84.     }
  85.     }
  86.  
  87.     /**
  88.      * Read characters into a portion of an array.
  89.      * @param b     Destination buffer
  90.      * @param off  Offset at which to start storing characters
  91.      * @param len   Maximum number of characters to read
  92.      * @return  The actual number of characters read, or -1 if
  93.      *         the end of the stream has been reached
  94.      * 
  95.      * @exception   IOException  If an I/O error occurs
  96.      */
  97.     public int read(char b[], int off, int len) throws IOException {
  98.     synchronized (lock) {
  99.         ensureOpen();
  100.             if ((off < 0) || (off > b.length) || (len < 0) ||
  101.                 ((off + len) > b.length) || ((off + len) < 0)) {
  102.                 throw new IndexOutOfBoundsException();
  103.             } else if (len == 0) {
  104.                 return 0;
  105.             }
  106.  
  107.         if (pos >= count) {
  108.         return -1;
  109.         }
  110.         if (pos + len > count) {
  111.         len = count - pos;
  112.         }
  113.         if (len <= 0) {
  114.         return 0;
  115.         }
  116.         System.arraycopy(buf, pos, b, off, len);
  117.         pos += len;
  118.         return len;
  119.     }
  120.     }
  121.  
  122.     /**
  123.      * Skip characters.
  124.      * @param n The number of characters to skip
  125.      * @return    The number of characters actually skipped
  126.      * 
  127.      * @exception   IOException  If an I/O error occurs
  128.      */
  129.     public long skip(long n) throws IOException {
  130.     synchronized (lock) {
  131.         ensureOpen();
  132.         if (pos + n > count) {
  133.         n = count - pos;
  134.         }
  135.         if (n < 0) {
  136.         return 0;
  137.         }
  138.         pos += n;
  139.         return n;
  140.     }
  141.     }
  142.  
  143.     /**
  144.      * Tell whether this stream is ready to be read.  Character-array readers
  145.      * are always ready to be read.
  146.      *
  147.      * @exception  IOException  If an I/O error occurs
  148.      */
  149.     public boolean ready() throws IOException {
  150.     synchronized (lock) {
  151.         ensureOpen();
  152.         return (count - pos) > 0;
  153.     }
  154.     }
  155.  
  156.     /**
  157.      * Tell whether this stream supports the mark() operation, which it does.
  158.      */
  159.     public boolean markSupported() {
  160.     return true;
  161.     }
  162.  
  163.     /**
  164.      * Mark the present position in the stream.  Subsequent calls to reset()
  165.      * will reposition the stream to this point.
  166.      *
  167.      * @param  readAheadLimit  Limit on the number of characters that may be
  168.      *                         read while still preserving the mark.  Because
  169.      *                         the stream's input comes from a character array,
  170.      *                         there is no actual limit; hence this argument is
  171.      *                         ignored.
  172.      *
  173.      * @exception  IOException  If an I/O error occurs
  174.      */
  175.     public void mark(int readAheadLimit) throws IOException {
  176.     synchronized (lock) {
  177.         ensureOpen();
  178.         markedPos = pos;
  179.     }
  180.     }
  181.  
  182.     /**
  183.      * Reset the stream to the most recent mark, or to the beginning if it has
  184.      * never been marked.
  185.      *
  186.      * @exception  IOException  If an I/O error occurs
  187.      */
  188.     public void reset() throws IOException {
  189.     synchronized (lock) {
  190.         ensureOpen();
  191.         pos = markedPos;
  192.     }
  193.     }
  194.  
  195.     /**
  196.      * Close the stream.
  197.      */
  198.     public void close() {
  199.     buf = null;
  200.     }
  201. }
  202.